//
//  BCUserCenterVC.m
//  BaoYouYou
//
//  Created by caobo56 on 2016/12/4.
//  Copyright © 2016年 caobo56. All rights reserved.
//

#import "BCUserCenterVC.h"
#import "BCUserCenterCell.h"
#import "BCAboutVC.h"
#import "BCKnowledgeVC.h"
#import "BCHelperVC.h"
#import "BCLoginVC.h"
#import "BCRegisterVC.h"

#import "UIView+toast.h"

@interface BCUserCenterVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *avatarImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLable;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *loginBtnPress;
@property (weak, nonatomic) IBOutlet UIButton *registerBtnPress;

@end

static NSString * const CellReuseIdentifier = @"BCUserCenterCell";


@implementation BCUserCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self settableViewStyle];
    
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    if ([[user valueForKey:@"isLogin"] intValue] == 1) {
        _userNameLable.text = [user valueForKey:@"phoneNo"];
        _userNameLable.hidden = NO;
        _loginBtnPress.hidden = YES;
        _registerBtnPress.hidden = YES;
    }else{
        _userNameLable.hidden = YES;
        _loginBtnPress.hidden = NO;
        _registerBtnPress.hidden = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)loginBtnPress:(id)sender {
    BCLoginVC * login = (BCLoginVC *)[[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"BCLoginVC"];
    login.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:login animated:YES];
}

- (IBAction)resigterBtnPress:(id)sender {
    BCRegisterVC * registerVC = (BCRegisterVC *)[[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"BCRegisterVC"];
    registerVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:registerVC animated:YES];
}

-(void)settableViewStyle{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setLayoutMargins:UIEdgeInsetsZero];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BCUserCenterCell * cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier];
    if (!cell) {
        cell = [BCUserCenterCell getBCUserCenterCell];
    }
    [cell loaDataWith:indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        BCHelperVC* helper = [BCHelperVC new];
        helper.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:helper animated:YES];
    }else if (indexPath.row == 1) {
        BCKnowledgeVC * knowledge = [BCKnowledgeVC new];
        knowledge.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:knowledge animated:YES];
    }else if (indexPath.row == 2) {
        BCAboutVC * about = [BCAboutVC new];
        about.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:about animated:YES];
    }else{
        _userNameLable.text = @"";
        _userNameLable.hidden = YES;
        _loginBtnPress.hidden = NO;
        _registerBtnPress.hidden = NO;
        
        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
        [user setValue:@(0) forKey:@"isLogin"];
        [user setValue:@"" forKey:@"phoneNo"];
        [user setValue:@"" forKey:@"password"];
        [user setValue:@"" forKey:@"email"];
        [user setValue:@"" forKey:@"userid"];
        [user synchronize];
        [self.view toastMessage:@"已经退出登录"];
    }
}



@end
